package com.lsx.logisticscore.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsx.logisticscore.entity.Account;
import com.lsx.logisticscore.entity.Car;
import com.lsx.logisticscore.entity.Employee;
import com.lsx.logisticscore.request.EmployeeVO;
import com.lsx.logisticscore.service.EmployeeService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

@Slf4j
@RestController
@RequestMapping()
public class MyController {

    @Autowired
    private EmployeeService employeeService;
    @PostMapping("/selectEmpByCondition")
    public IPage<Employee> doGetEmployee(Page<Employee> page, EmployeeVO vo){
        return employeeService.getEmployee(page, vo);
    }

    @RequestMapping("/test")
    public String doTest(@RequestBody Account account) throws IOException {
        log.info("获取到的数据：{}", account);
       return "hello world!";

    }


    @RequestMapping("/testMap")
    public Map<String, Object> doTestMap(){
        Map<String, Object> map = new HashMap<>();
        map.put("1", "1");
        map.put("2", null);
        return map;
    }



    public static void main(String[] args) throws IOException {

        List<Car> cars = new ArrayList<>();
        Car car1 = new Car();
        car1.setCarId(12);
        car1.setCarType("da汽车");

        Car car2 = new Car();
        car2.setCarId(123);
        car2.setCarType("小汽车");

        cars.add(car1);
        cars.add(car2);

        File F=new File("D:/file.txt");
        //如果文件存在,先删除
        if(F.exists()){
            F.delete();
        }
        //如果文件不存在,就动态创建文件
        if(!F.exists()){
            F.createNewFile();
        }
        FileWriter fw=null;
        //设置为:True,表示写入的时候追加数据
        fw=new FileWriter(F, true);
        for (Car car:cars
        ) {
            StringBuilder sb = new StringBuilder();
            sb.append(car.getCarId()).append(" ");
            sb.append(car.getCarType()).append(" ");
            sb.append("\r\n");
            System.out.println(sb.toString());
            try {

                fw.write(sb.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if(fw!=null){
            fw.close();
        }
    }
}
